Trying to simplify the code

Made new functions, changed some code.