Fixes errors and cleans up code