working version and accepted at online judge