checking logic and fixing errors