refactor and a little enhancements