Separation of functions and classes

@wkerzend: I thought it would be better to split the basics into functions that are fairly clear and simple, and the wrapper classes which may need more thought.