Added initial sparse implementation with some refactoring