my attempt at cleaning the code