Help to cleanup this code

get_post returns a WP_Post object, so you can retrieve the post parent with $post->post_parent. You should have a look at the member variables in the WP_Post link.