Cleaning up variable names and fixing some logic