added gain functionality, fixed a bug