Code cleanup

Formatted code and added few little optimizations.