misc cleanup and refactoring
fix some edge case bugs with lines and cleanup more code