Added essential functions, cleaned up the code