Replaced default gain function with own loop