working version

before optimizing