Removing global variables and correcting mistakes