Added check for username and some code cleaning.