Replaced code fragments to functions