Major refactor to make consistent with lib convention
Bug fix