Fixed issue with writing files + some cleanup.