code cleanup, fix some little bugs