somewhat functional version, still full of debugging portions