Cleaned and fixed code