Update code

Created seperated functions, optimized code