cleaning up and unifying the code