Implemented functions so script looks cleaner and it should work