clean up and optimze code