WIP: refactoring: extract functions in heartbeart code

in order to avoid nasty bugs in the future caused by copy and paste