Reduced the amount of duplicated code

part of #143.