Various fixes and adding tests