reformated code, added some test