fixed several bugs introduced from refactor