Added main script; fixed bugs