refactored some ugly pattern matches into active-patterns