Refactored code, seperated functions etc