Merged in changes from cristirusu, did some additional cleanup

Merged in changes from cristirusu, did some additional cleanup