improved type checking and fixing a few bugs