first revision
fixed a bug and added readable comments