Solved some TODOs. Added some sanity checks