removed some redundancy
added main and fixed a bug
merged with master