Fix bug; refactor