Cleaned code and added a function