TMX code cleanup

Some merging and cleanup in TMX code, also added some comments