refactored to use prototyping and generally make the library faster