fixed a lot of bugs and added command-line options