bug fix and cleanup