Moved code to its own method to reduce method length.
Adds comments.