slight refactor and fix some minor bugs