turned more stuff into function

made code a bit easier to read and a bit more efficient too