Initial implementation

First comment: tons of one and two-letter variable names here. Not at all friendly!