[bw] refactored code, organized common functions across problems