Initial implementation attempt.
More optimizations and implementation.