Refactored out common codes for reuse, moved out of sample package