Cleaned code and updated the function definition