refactor and bug fixes in response