Code cleanup and fixes

Still needs some more fixes before commiting to IDPF.