Cleaned and re-factored code