simplified/improved some code