Fixed selectmany bug + refactoring