Index: pages/api/blackjack/calculateEarnings.js
===================================================================
--- pages/api/blackjack/calculateEarnings.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
+++ pages/api/blackjack/calculateEarnings.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
@@ -0,0 +1,71 @@
+import { checkIfSameValue, checkIfSameColour, checkIfSameSuit, checkIfStraight } from './checkCardCombinations'
+
+export function calculateEarnings(room) {
+    let betEarnings = 0;
+
+    if (room.outcome === 'draw') {
+        betEarnings = room.initialBet;
+    }
+    else if (room.outcome === 'player_won' || room.outcome === 'dealer_busted') {
+        betEarnings = 2 * room.initialBet;
+    }
+    else if (room.outcome === 'player_lost' || room.outcome === 'player_busted') {
+        betEarnings = -1 * room.initialBet;
+    }
+
+    return betEarnings;
+}
+  
+export function calculateSideBetEarnings(room) {
+    let sideBetEarnings = -1 * room.sideBet;
+  
+    if (room.sideBetName != '') {
+      if (room.sideBetName === 'mixed_pair') {
+        if (checkIfSameValue(room.playerCards)) {
+          sideBetEarnings = room.sideBet * 5;
+        }
+      }
+      else if (room.sideBetName === 'coloured_pair') {
+        if (checkIfSameValue(room.playerCards) && checkIfSameColour(room.playerCards)) {
+          sideBetEarnings = room.sideBet * 12;
+        }
+      }
+      else if (room.sideBetName === 'perfect_pair') {
+        if (checkIfSameValue(room.playerCards) && checkIfSameSuit(room.playerCards)) {
+          sideBetEarnings = room.sideBet * 25;
+        }
+      }
+      else if (room.sideBetName === 'flush') {
+        const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
+        if (checkIfSameSuit(tmpCards)) {
+          sideBetEarnings = room.sideBet * 5;
+        }
+      }
+      else if (room.sideBetName === 'straight') {
+        const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
+        if (checkIfStraight(tmpCards)) {
+          sideBetEarnings = room.sideBet * 10;
+        }
+      }
+      else if (room.sideBetName === 'three_of_a_kind') {
+        const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
+        if (checkIfSameValue(tmpCards)) {
+          sideBetEarnings = room.sideBet * 30;
+        }
+      }
+      else if (room.sideBetName === 'straight_flush') {
+        const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
+        if (checkIfStraight(tmpCards) && checkIfSameSuit(tmpCards)) {
+          sideBetEarnings = room.sideBet * 40;
+        }
+      }
+      else if (room.sideBetName === 'suited_triple') {
+        const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
+        if (checkIfSameSuit(tmpCards) && checkIfSameValue(tmpCards)) {
+          sideBetEarnings = room.sideBet * 100;
+        }
+      }
+    }
+  
+    return sideBetEarnings;
+}
Index: pages/api/blackjack/checkCardCombinations.js
===================================================================
--- pages/api/blackjack/checkCardCombinations.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
+++ pages/api/blackjack/checkCardCombinations.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
@@ -0,0 +1,88 @@
+export function checkIfSameValue(cards) {
+    for (let i = 1; i < cards.length; i++) {
+      if (cards[i][1] !== cards[i-1][1]) {
+        return false;
+      }
+    }
+  
+    return true;
+}
+  
+function checkIf2CardsAreSameColour(card1, card2) {
+    if (card1[0] === card2[0]) return true;
+    if (card1[0] === 'H' && card2[0] === 'D') return true;
+    if (card1[0] === 'D' && card2[0] === 'H') return true;
+    if (card1[0] === 'S' && card2[0] === 'C') return true;
+    if (card1[0] === 'C' && card2[0] === 'S') return true;
+    return false;
+}
+  
+export function checkIfSameColour(cards) {
+    for (let i = 1; i < cards.length; i++) {
+      if (!checkIf2CardsAreSameColour(cards[i], cards[i-1])) {
+        return false;
+      }
+    }
+  
+    return true;
+}
+  
+export function checkIfSameSuit(cards) {
+    for (let i = 1; i < cards.length; i++) {
+      if (cards[i][0] !== cards[i-1][0]) {
+        return false;
+      }
+    }
+  
+    return true;
+}
+  
+export function checkIfStraight(cards) {
+    let values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'];
+  
+    let valuesInCards = [];
+    for (let i = 0; i < cards.length; i++) {
+      valuesInCards.push(cards[i][1]);
+    }
+  
+    let temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
+  
+    valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
+  
+    let idx = values.indexOf(valuesInCards[0]);
+  
+    let straight = true;
+  
+    for (let i = 0; i < valuesInCards.length; i++) {
+      if (valuesInCards[i] !== values[idx]) {
+        straight = false;
+        break; 
+      }
+  
+      idx++;
+      if (idx >= temp.length) {
+        straight = false;
+        break;
+      }
+    }
+  
+    if (straight) {
+      return true;
+    }
+  
+    values = ['2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'A'];
+    temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
+  
+    valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
+  
+    idx = values.indexOf(valuesInCards[0]);
+  
+    for (let i = 0; i < valuesInCards.length; i++) {
+      if (valuesInCards[i] !== values[idx]) return false;
+  
+      idx++;
+      if (idx >= temp.length) return false;
+    }
+  
+    return true;
+}
Index: pages/api/blackjack/gameStates.js
===================================================================
--- pages/api/blackjack/gameStates.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
+++ pages/api/blackjack/gameStates.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
@@ -0,0 +1,82 @@
+const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
+                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
+                    "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
+                    "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
+
+/* We are using 5 decks */
+const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
+
+/**
+ * Sample game state (Begining of the game)
+ */
+export let game = {
+  deck: [...deck],
+  status: '_1_room_created',
+  playerCards: [],
+  dealerName: 'Lazar',
+  dealerCards: [],
+  initialBet: 0,
+  sideBet: 0,
+  sideBetName: '',
+  outcome: '',
+  earnings: 0,
+  sideBetOutcome: '',
+  sideBetEarnings: 0,
+}
+
+/**
+ * Replace deck if empty
+ */
+function checkDeckSize(game) {
+    if (game.deck.length === 0) {
+      game.deck = [...deck];
+    }
+  }
+  
+/**
+ * Draw a SINGLE random card
+ */
+export function drawASingleCard(room) {
+    checkDeckSize(room);
+    let idx = Math.floor(Math.random() * room.deck.length);
+    let card = room.deck[idx];
+  
+    room.deck.splice(idx, 1);
+  
+    return card;
+}
+  
+/**
+ * Deal the initial hand of cards
+ */
+export function getInitialCards(room) {
+    room.playerCards.push(drawASingleCard(room));
+    room.playerCards.push(drawASingleCard(room));
+  
+    room.dealerCards.push(drawASingleCard(room));
+    room.dealerCards.push(drawASingleCard(room));
+}
+
+/**
+ * Calculate the hand value
+ */
+export function calculateHandValue(cards) {
+    let value = 0;
+    let aces = 0;
+    for (let i = 0; i < cards.length; i++) {
+      let card = cards[i];
+      if (card.substring(1) === 'A') {
+        value += 11;
+        aces++;
+      } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
+        value += 10;
+      } else {
+        value += parseInt(card.substring(1));
+      }
+    }
+    while (value > 21 && aces > 0) {
+      value -= 10;
+      aces--;
+    }
+    return value;
+}
Index: pages/api/blackjack/index.js
===================================================================
--- pages/api/blackjack/index.js	(revision 64dc53b0a494541c0f886c0ddbf1a83488532825)
+++ pages/api/blackjack/index.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
@@ -1,139 +1,10 @@
-import { v4 as uuidv4 } from 'uuid';
-
 import axios from 'axios';
 
 require('dotenv').config();
 
-/**
- * ********************* BEGIN OF DEALING WITH GAME STATES *********************
- */
-
-const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
-                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
-                    "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
-                    "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
-
-/* We are using 5 decks */
-const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
-
-let game = {
-  deck: [...deck],
-  status: '_1_room_created',
-  playerCards: [],
-  dealerName: 'Lazar',
-  dealerCards: [],
-  initialBet: 0,
-  sideBet: 0,
-  sideBetName: '',
-  outcome: '',
-  earnings: 0,              // positive for draw, 2x for win, negative for loss.
-  sideBetOutcome: '',
-  sideBetEarnings: 0,
-}
+import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';
+import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
 
 let rooms = []
-
-/**
- * Replace deck if empty
- */
-function checkDeckSize(game) {
-  if (game.deck.length === 0) {
-    game.deck = [...deck];
-  }
-}
-
-/**
- * Draw a SINGLE random card
- */
-function drawASingleCard(room) {
-  checkDeckSize(room);
-  let idx = Math.floor(Math.random() * room.deck.length);
-  let card = room.deck[idx];
-
-  room.deck.splice(idx, 1);
-
-  return card;
-}
-
-/**
- * Deal the initial hand of cards
- */
-function getInitialCards(room) {
-  room.playerCards.push(drawASingleCard(room));
-  room.playerCards.push(drawASingleCard(room));
-
-  room.dealerCards.push(drawASingleCard(room));
-  room.dealerCards.push(drawASingleCard(room));
-}
-
-function calculateEarnings(room) {
-  let betEarnings = 0;
-
-  if (room.outcome === 'draw') {
-    betEarnings = room.initialBet;
-  }
-  else if (room.outcome === 'player_won' || room.outcome === 'dealer_busted') {
-    betEarnings = 2 * room.initialBet;
-  }
-  else if (room.outcome === 'player_lost' || room.outcome === 'player_busted') {
-    betEarnings = -1 * room.initialBet;
-  }
-
-  return betEarnings;
-}
-
-function calculateSideBetEarnings(room) {
-  let sideBetEarnings = -1 * room.sideBet;
-
-  if (room.sideBetName != '') {
-    if (room.sideBetName === 'mixed_pair') {
-      if (checkIfSameValue(room.playerCards)) {
-        sideBetEarnings = room.sideBet * 5;
-      }
-    }
-    else if (room.sideBetName === 'coloured_pair') {
-      if (checkIfSameValue(room.playerCards) && checkIfSameColour(room.playerCards)) {
-        sideBetEarnings = room.sideBet * 12;
-      }
-    }
-    else if (room.sideBetName === 'perfect_pair') {
-      if (checkIfSameValue(room.playerCards) && checkIfSameSuit(room.playerCards)) {
-        sideBetEarnings = room.sideBet * 25;
-      }
-    }
-    else if (room.sideBetName === 'flush') {
-      const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
-      if (checkIfSameSuit(tmpCards)) {
-        sideBetEarnings = room.sideBet * 5;
-      }
-    }
-    else if (room.sideBetName === 'straight') {
-      const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
-      if (checkIfStraight(tmpCards)) {
-        sideBetEarnings = room.sideBet * 10;
-      }
-    }
-    else if (room.sideBetName === 'three_of_a_kind') {
-      const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
-      if (checkIfSameValue(tmpCards)) {
-        sideBetEarnings = room.sideBet * 30;
-      }
-    }
-    else if (room.sideBetName === 'straight_flush') {
-      const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
-      if (checkIfStraight(tmpCards) && checkIfSameSuit(tmpCards)) {
-        sideBetEarnings = room.sideBet * 40;
-      }
-    }
-    else if (room.sideBetName === 'suited_triple') {
-      const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
-      if (checkIfSameSuit(tmpCards) && checkIfSameValue(tmpCards)) {
-        sideBetEarnings = room.sideBet * 100;
-      }
-    }
-  }
-
-  return sideBetEarnings;
-}
 
 /**
@@ -147,7 +18,4 @@
   rooms[session_id] = room;
 }
-/**
- * ********************* END OF DEALING WITH GAME STATES *********************
- */
 
 /**
@@ -161,4 +29,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * If game status is _5_game_over, restart the room for a new game.
      * @action play_again
      * @param session_id
@@ -185,4 +54,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * If game status is _4_cards_on_the_table, draw cards for the dealer while handValue < 17, and calculate game outcome and player earnings.
+     * Also, update the player's credits and stats in the database through /api/postgre?action=add_credits.
      * @action stand
      * @param session_id
@@ -246,4 +117,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * If game status is _4_cards_on_the_table, draw a card for the player.
+     * If player busts, update the player's stats in the database through /api/postgre?action=add_credits.
      * @action hit_a_card
      * @param session_id
@@ -305,4 +178,6 @@
      /**
      * /---------------------- GET ----------------------/
+     * If game status is _3_made_side_bet, check if the player won the side bet or not (if they placed a side bet of course).
+     * Update the player's stats in the database through /api/postgre?action=add_credits.
      * @action get_initial_cards
      * @param session_id
@@ -366,4 +241,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * If game status is _2_made_initial_bet, place a side bet if the user has chosen one.
      * @action make_side_bet
      * @param session_id
@@ -402,4 +278,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * If game status is _1_room_created, get the initial bet placed by the player.
      * @action make_initial_bet
      * @param session_id
@@ -434,4 +311,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * Remove a room from the rooms array.
      * @action remove_room
      * @param session_id
@@ -451,4 +329,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * If the player is not in an existing room, create a room for them.
+     * If they are reconnecting, get the room they were in.
      * @action get_player_info_on_enter
      * @param session_id
@@ -465,5 +345,5 @@
 
       let dealerCardsTmp = [];
-      if (rooms[session_id].status.substr(1, 1) != '1') { // 5 == game_over
+      if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
         rooms[session_id].dealerCards.forEach((card, i) => {
           if (i === 0) {
@@ -474,4 +354,7 @@
           }
         })
+      }
+      else {
+        dealerCardsTmp = rooms[session_id].dealerCards;
       }
 
@@ -493,119 +376,2 @@
  * ********************* END OF REQUEST HANDLER *********************
  */
-
-/**
- * ********************* BEGIN OF FUNCTIONS THAT CHECK CARD COMBINATIONS *********************
- */
-
-function calculateHandValue(cards) {
-  let value = 0;
-  let aces = 0;
-  for (let i = 0; i < cards.length; i++) {
-    let card = cards[i];
-    if (card.substring(1) === 'A') {
-      value += 11;
-      aces++;
-    } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
-      value += 10;
-    } else {
-      value += parseInt(card.substring(1));
-    }
-  }
-  while (value > 21 && aces > 0) {
-    value -= 10;
-    aces--;
-  }
-  return value;
-}
-
-function checkIfSameValue(cards) {
-  for (let i = 1; i < cards.length; i++) {
-    if (cards[i][1] !== cards[i-1][1]) {
-      return false;
-    }
-  }
-
-  return true;
-}
-
-function checkIf2CardsAreSameColour(card1, card2) {
-  if (card1[0] === card2[0]) return true;
-  if (card1[0] === 'H' && card2[0] === 'D') return true;
-  if (card1[0] === 'D' && card2[0] === 'H') return true;
-  if (card1[0] === 'S' && card2[0] === 'C') return true;
-  if (card1[0] === 'C' && card2[0] === 'S') return true;
-  return false;
-}
-
-function checkIfSameColour(cards) {
-  for (let i = 1; i < cards.length; i++) {
-    if (!checkIf2CardsAreSameColour(cards[i], cards[i-1])) {
-      return false;
-    }
-  }
-
-  return true;
-}
-
-function checkIfSameSuit(cards) {
-  for (let i = 1; i < cards.length; i++) {
-    if (cards[i][0] !== cards[i-1][0]) {
-      return false;
-    }
-  }
-
-  return true;
-}
-
-function checkIfStraight(cards) {
-  let values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'];
-
-  let valuesInCards = [];
-  for (let i = 0; i < cards.length; i++) {
-    valuesInCards.push(cards[i][1]);
-  }
-
-  let temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
-
-  valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
-
-  let idx = values.indexOf(valuesInCards[0]);
-
-  let straight = true;
-
-  for (let i = 0; i < valuesInCards.length; i++) {
-    if (valuesInCards[i] !== values[idx]) {
-      straight = false;
-      break; 
-    }
-
-    idx++;
-    if (idx >= temp.length) {
-      straight = false;
-      break;
-    }
-  }
-
-  if (straight) {
-    return true;
-  }
-
-  values = ['2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'A'];
-  temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
-
-  valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
-
-  idx = values.indexOf(valuesInCards[0]);
-
-  for (let i = 0; i < valuesInCards.length; i++) {
-    if (valuesInCards[i] !== values[idx]) return false;
-
-    idx++;
-    if (idx >= temp.length) return false;
-  }
-
-  return true;
-}
-/**
- * ********************* END OF FUNCTIONS THAT CHECK CARD COMBINATIONS *********************
- */
Index: pages/api/postgre/index.js
===================================================================
--- pages/api/postgre/index.js	(revision 64dc53b0a494541c0f886c0ddbf1a83488532825)
+++ pages/api/postgre/index.js	(revision ebf5e04ad50c2ebbd0adf24c814a57cae1a7a172)
@@ -13,13 +13,5 @@
 
 const sessions = []
-// example session
-// const session = {
-//   id,
-//   displayName,
-//   username,
-//   credits,
-//   lastActivity,
-// }
-
+// example session = { id, displayName, username, credits, lastActivity }
 
 export default function handler(req, res) {
@@ -30,4 +22,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * If the player won credits, update them in the database.
+     * Also, update the stats in the database.
      * @action give_credits
      * @param session_id
@@ -95,4 +89,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * The player lost credits, update this in the database.
      * @action take_credits
      * @param session_id
@@ -138,4 +133,5 @@
     /**
      * /---------------------- GET ----------------------/
+     * Get stats for the player, so we can display them in the front end.
      * @action get_stats
      * @param session_id
@@ -174,4 +170,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * Returns the player's room, if the player was in one.
+     * Same as the one below, but this one is used in a game-specific context.
      * @action get_player_info_on_enter
      * @param session_id
@@ -198,4 +196,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * Returns the player's room, if the player was in one.
+     * Same as the one above, but this one is used in a general context.
      * @action check_if_logged_in
      * @param session_id
@@ -222,4 +222,6 @@
     /**
      * /---------------------- GET ----------------------/
+     * Takes the credits in the player's session, and updates the database.
+     * Logs the player out and kills the session.
      * @action logout
      * @param session_id
@@ -254,4 +256,5 @@
     /**
      * /---------------------- POST ----------------------/
+     * Checks if the entered account info is good, and registers a new user in the database if so.
      * @action register
      * @param username
@@ -339,4 +342,5 @@
     /**
      * /---------------------- POST ----------------------/
+     * Checks if the entered account info is good, and logs the user in if so.
      * @action login
      * @param username
@@ -433,10 +437,3 @@
     }
   }
-
-  /**
-   * PUT method
-   */
-  if (req.method === 'PUT') {
-
-  }
 }
