Split code to functions
Use bool instead of numbers in while loop