fixed some random errors and refactored more code