tidied code and fixed some TODOs