Cleaned up the code and rearaged some code(plus renaming)