Adjusting some logic and fixing error