cleaned up code, ready for use in Kdown