Cleaned up the code

Cleaned up the code, fixe some issues with last merge