logic and bug fixes