More fixes and code condensing