Some refactor and cleanup