Cleaned code and refactored a bit