first working version. needs cleanup