Logic changes and bug fixes