Error checking, cleanups, formatting line endings correctly