cleaned and improved some code