Simplified code, added docs, made exported/non-exported methods.