Fixed some functionality and cleaned up the code