Cleaned up and improved the code

Please see the .inc file for native changes