added small improvements to logic